﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using patents_robot_demo.HttpMethod;
using patents_robot_demo.Analysis;
using patents_robot_demo.DataBase;
namespace patents_robot_demo.NetWorkOperate
{
    class GainHTML
    {      
        #region POST 获得专利号码html 两个参数版本
        /// <summary>
        ///  POST 获得专利号码html 两个参数版本
        /// </summary>
        /// <param name="ApplyPeople">申请人</param>
        /// <param name="n">一次请求返回的专利号码数量</param>
        /// <returns>n个专利号码</returns>
        static public string RecPatent(string ApplyPeople, int n)
        {
            HttpClient clinet = new HttpClient();
            string postcode1 = @"recshu="
                + Convert.ToString(n + 1).PadLeft(10, '0')
                + @"&searchword=%C9%EA%C7%EB%A3%A8%D7%A8%C0%FB%C8%A8%A3%A9%C8%CB%3D%28";
            string postcode2 = @"%29+&flag3=1&pg=1&sign=0&textfield3=&textfield9=&textfield10=&textfield12=&textfield4=&textfield5=&textfield2=&textfield11=&textfield6=%C9%BD%B6%AB%C0%ED%B9%A4%B4%F3%D1%A7&textfield7=&textfield8=&gjgb=&textfield13=&textfield141=&textfield142=&textfield143=";
            string postcode = postcode1 + Security.GetGB2312(ApplyPeople) + postcode2;
            string recinfo = null;
            try
            {
                recinfo = clinet.PostAndGetHtml(@"http://211.157.104.87:8080/sipo/zljs/hyjs-jieguo.jsp",
                    postcode,
                    @"application/x-www-form-urlencoded",
                    @"http://www.sipo.gov.cn/zljs/",
                    false,
                    Encoding.GetEncoding("GB2312")
                    );
            }
            catch (Exception ex)
            {
                DataBase.DBWork.SaveLogInformation(ex.Message);
            }
            if (recinfo == null) return RecPatent(ApplyPeople, n);
            else return recinfo;
        }
        #endregion
        #region POST 获得专利号码html 超强版 四个参数版本
        /// <summary>
        /// 
        /// </summary>
        /// <param name="ApplyPeople"></param>
        /// <param name="Total"></param>
        /// <param name="Finished"></param>
        /// <param name="Speed"></param>
        /// <returns></returns>
        static private string GetPatentIdHTML(string ApplyPeople, int Total, int Finished, int Speed)
        {
            string html = null;
            HttpClient clinet = new HttpClient();
            string postcode = @"flag3=1&selectbase=0&sign=0&recshu="
                + Convert.ToString(Speed).PadLeft(10, '0')
                + "&searchword=%C9%EA%C7%EB%A3%A8%D7%A8%C0%FB%C8%A8%A3%A9%C8%CB%3D%2"
                + Security.GetGB2312(ApplyPeople)
                + "%29+&pg="
                + Convert.ToString(Finished / Speed + 1).PadLeft(10, '0');
            string recinfo = null;
            try
            {
                html = clinet.PostAndGetHtml(
                    @"http://211.157.104.87:8080/sipo/zljs/hyjs-jieguo.jsp",
                    postcode,
                    @"application/x-www-form-urlencoded",
                    @"http://www.sipo.gov.cn/zljs/",
                    false,
                    Encoding.GetEncoding("GB2312")
                    );
            }
            catch (Exception ex)
            {
                DataBase.DBWork.SaveLogInformation(ex.Message);
            }
            if (recinfo == null) return GetPatentIdHTML(ApplyPeople, Total, Finished, Speed);
            else return html;
        }
        #endregion
        #region GET 获得专利详细信息 html
        /// <summary>
        /// 网络操作 得到专利的详细信息
        /// </summary>
        /// <param name="PatentID">专利号码</param>
        /// <returns>返回详细的专利信息</returns>
        static public string GetPatentInfo(string PatentID)
        {
            HttpClient client = new HttpClient();
            string gethtml = null;
            try
            {
                gethtml = client.GetAndGetHtml("http://211.157.104.87:8080/sipo/zljs/hyjs-yx-new.jsp?recid=CN" + PatentID,
                    @"application/x-www-form-urlencoded",
                    @"http://211.157.104.87:8080/sipo/zljs/hyjs-jieguo.jsp",
                    false,
                    Encoding.GetEncoding("GB2312")
                    );
            }
            catch (Exception ex)
            {
                DataBase.DBWork.SaveLogInformation(ex.Message);
            }
            if (gethtml == null) return GetPatentInfo(PatentID);
            else return gethtml;
        }
        #endregion
        #region POST 获得法律状态html
        /// <summary>
        /// 网络操作 获得法律状态HTML
        /// </summary>
        /// <param name="PatentID"></param>
        /// <returns></returns>
        static private string PostLawState(string PatentID)
        {
            HttpClient client = new HttpClient();
            string postcode = @"textfield1=" + PatentID + "&textfield4=&textfield5=&searchword=%C9%EA%C7%EB%BA%C5%3D200710113974.0%25+";
            string html = null;

            try
            {
                html = client.PostAndGetHtml(@"http://search.sipo.gov.cn/sipo/zljs/FlztResult.jsp",
                    postcode,
                    @"application/x-www-form-urlencoded",
                    @"http://search.sipo.gov.cn/sipo/zljs/searchflzt.jsp",
                    false,
                    Encoding.GetEncoding("GB2312")
                    );
            }
            catch (Exception ex)
            {
                DataBase.DBWork.SaveLogInformation(ex.Message);
            }
            if (html == null) return PostLawState(PatentID);
            else return html;
        }
        #endregion
    }
}
